package com.sq.partner.manager.sso;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.log4j.Logger;

import com.sq.partner.manager.common.Config;
import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.sso.webservice.ServiceException_Exception;
import com.sq.partner.manager.sso.webservice.UserInfo;
import com.sq.partner.manager.sso.webservice.UserService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.webservice.LifeCoreClient;

public class UMClient {
	private static Logger logger = LogUtil.getLog(LifeCoreClient.class);
	private static UserService userService = null;
	static {
		try {
			String url = Config.getProp("UM_WEBSERVICE_URL");
			logger.info("UM webservice请求地址:" + url);
			// 创建WebService客户端代理工厂
			JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
			// 注册WebService接口
			factory.setServiceClass(UserService.class);
			// 设置WebService地址
			factory.setAddress(url);
			userService = (UserService) factory.create();
		} catch (Exception e) {
			logger.error("UM webservice URL 初使化失败", e);
		}
		logger.info("UM webservice URL 初使化成功");
	}

	/**
	 * 查询user信息
	 * 
	 * @param userId
	 * @return 不存在返回null
	 * @throws ServiceException_Exception
	 */
	public static UserInfo getUser(String userId)
			throws ServiceException_Exception {
		return userService.getUser(userId);
	}

	/**
	 * 用户认证
	 * 
	 * @param userId
	 * @return 成功为true，失败为false
	 * @throws ServiceException_Exception
	 */
	public static boolean authenticate(String userId, String password,
			String loginIp) throws ServiceException_Exception {
		return userService.authenticate(userId, password,
				Constants.APP_ID_IN_UM, loginIp);
	}

	/**
	 * 用户认证并返回用户信息
	 * 
	 * @param userId
	 * @return 返回null时验证失败，成功时返回用户信息
	 * @throws ServiceException_Exception
	 */
	public static UserInfo authBackInfo(String userId, String password,
			String loginIp) throws ServiceException_Exception {
		return userService.authBackInfo(userId, password,
				Constants.APP_ID_IN_UM, loginIp);
	}

	public static void main(String[] args) throws ServiceException_Exception {
		String[] users = { "lipengfei001", "chenqun001", "jingjian001",
				"sunyiping001" };
		for (String s : users) {
			UserInfo userInfo = userService.getUser(s);
			System.out.println(userInfo.getUserName());
		}

	}
}
